package com.netdisk.controller;


import com.netdisk.Domain.FileBean;
import com.netdisk.Domain.User;
import com.netdisk.component.FileDealCode;
import com.netdisk.dto.file.*;
import com.netdisk.service.HDFSService;

import com.qiwenshare.common.result.RestResult;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import org.apache.hadoop.fs.FileSystem;
import org.springframework.web.bind.annotation.*;


import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import javax.validation.Valid;
import java.io.IOException;
import java.util.List;

@RestController
@RequestMapping("/file")
public class FileController {
    @Resource
    HDFSService hdfsService;

    @Resource
    FileDealCode fileDealCode;

    @Resource
    FileSystem fileSystem;

    @Operation(summary = "获取文件列表", description = "用来做前台列表展示", tags = {"file"})
    @RequestMapping(value = "/getfilelist", method = RequestMethod.GET)
    @ResponseBody
    public RestResult<List<FileBean>> getFileList(
            @Parameter(description = "文件类型", required = true) String fileType,
            @Parameter(description = "文件路径", required = true) String filePath,
            @Parameter(description = "当前页", required = true) long currentPage,
            @Parameter(description = "页面数量", required = true) long pageCount,
            HttpServletRequest request) throws IOException {
        if ("0".equals(fileType)) {
            List<FileBean> list = hdfsService.getListFile(filePath, request);
            return RestResult.success().dataList(list, list.size());
        } else {
            List<FileBean> list = hdfsService.listFileByType(Integer.parseInt(fileType), filePath, request);
            return RestResult.success().dataList(list, list.size());
        }
    }

    @Operation(summary = "文件搜索", description = "文件搜索", tags = {"file"})
    @GetMapping(value = "/search")
    @ResponseBody
    public RestResult<List<FileBean>> searchFile(SearchFileDTO searchFileDTO, HttpServletRequest request) {
        List<FileBean> list = hdfsService.getListByName(request, searchFileDTO.getFileName());
        if (list != null) {
            return RestResult.success().dataList(list, list.size());
        } else {
            return RestResult.fail().message("出现未知错误");
        }
    }

    @Operation(summary = "创建文件夹", description = "目录(文件夹)的创建", tags = {"file"})
    @RequestMapping(value = "/createFold", method = RequestMethod.POST)
    @ResponseBody
    public RestResult<String> createFold(@Valid @RequestBody CreateFoldDTO createFoldDto, HttpServletRequest request) throws IOException {

        HttpSession session = request.getSession();
        User user = (User) session.getAttribute("user");
        String telephone = user.getTelephone();
        String filePath = "/" + telephone + createFoldDto.getFilePath() + "/" + createFoldDto.getFileName();
        if (hdfsService.existFile(filePath)) {
            return RestResult.fail().message("同名文件夹已存在");
        }
        hdfsService.makeFolder(filePath);
        return RestResult.success();
    }

    @Operation(summary = "文件重命名", description = "文件重命名", tags = {"file"})
    @RequestMapping(value = "/renamefile", method = RequestMethod.POST)
    @ResponseBody
    public RestResult<String> renameFile(@RequestBody RenameFileDTO renameFileDto, HttpServletRequest request) {
        HttpSession session = request.getSession();
        User user = (User) session.getAttribute("user");
        String filePath = renameFileDto.getUserFileId();
        String oldFilePath = null;
        String newFilePath = null;
        String oldPath = (String) session.getAttribute("filePath");
        if (filePath.contains(oldPath)) {
            oldFilePath = filePath;
            newFilePath = filePath.substring(0, filePath.lastIndexOf("/") + 1) +
                    renameFileDto.getFileName() + filePath.substring(filePath.lastIndexOf("."));
        } else {
            oldFilePath = oldPath + "/" + renameFileDto.getUserFileId();
            if (filePath.lastIndexOf(".") == -1) {
                newFilePath = newFilePath = oldPath + renameFileDto.getFileName();
            } else {
                newFilePath = oldPath + "/" + renameFileDto.getFileName() + filePath.substring(filePath.lastIndexOf("."));
            }
        }
        if (hdfsService.existFile(newFilePath)) {
            return RestResult.fail().message("同名文件已存在");
        }
        hdfsService.renameFile(oldFilePath, newFilePath);
        return RestResult.success();
    }

    @Operation(summary = "删除文件", description = "可以删除文件或者目录", tags = {"file"})
    @RequestMapping(value = "/deletefile", method = RequestMethod.POST)
    @ResponseBody
    public RestResult deleteFile(@RequestBody DeleteFileDTO deleteFileDto,HttpServletRequest request) {
        HttpSession session = request.getSession();
        String oldFilePath = (String)session.getAttribute("filePath");
        String filePath = deleteFileDto.getUserFileId();
        String deleteFilePath = null;
        if (filePath.contains(oldFilePath)) {
            deleteFilePath = filePath;
        } else {
            deleteFilePath = oldFilePath + "/" + filePath;
        }
        if(hdfsService.deleteFile(deleteFilePath)){
            return RestResult.success();
        }else{
            return RestResult.fail().message("删除失败");
        }
    }

}
